﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta HTTP-EQUIV="pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<meta HTTP-EQUIV="expires" CONTENT="0">
<head>
  <META content="text/html; charset=utf-8" http-equiv="Content-Type">
  <TITLE>Facebook接入示例</TITLE>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
  <script>
	var access_token="";
	var user_id="";
	var live_video_url="";
	var live_video_id="";

	$(document).ready(function() {
		$.ajaxSetup({ cache: true });
		$.getScript('//connect.facebook.net/en_US/sdk.js', function(){
			FB.init({
				appId: '135910117007096',
				version: 'v2.10' // or v2.1, v2.2, v2.3, ...
			}); 
		});
	});
	
	function facebook_access_token()
	{
		FB.login(function(response) {
			if (response.authResponse) {
				console.log('Welcome!  Fetching your information.... ');
				user_id = response.authResponse.userID;
				access_token = response.authResponse.accessToken;

				$("#login_info").html("user_id:"+user_id+"<br/>token="+access_token);

				facebook_live_videos();
			} else {
				alert("Facebook login failed!");
			}
		}, {scope: 'publish_actions,manage_pages,publish_pages'});
	}

	function facebook_live_videos()
	{
		FB.api(
		  '/' + user_id + '/live_videos',
		  'POST',
		  {},
		  function(response) {
			  live_video_id=response.id;
			  live_video_url=response.stream_url;
			  $("#login_info").html("live_id:"+live_video_id+"<br/>live_url="+live_video_url);
			  
			  //post message all pages
			  window.parent.postMessage(live_video_url,'*'); 
		  }
		);
	}

	function facebook_delete_live()
	{
		FB.api(
		  '/' + live_video_id,
		  'DELETE',
		  {},
		  function(response) {
			  if(response.success==true){
				  //删成功
				  $("#del_info").html("delete live id:"+live_video_id);
			  }
		  }
		);
	}

  </script>


</head>

<body>

	<table width="600px" height="80px">
	<tr>
		<td><input type="button" style="background-image:url(facebook-login.png);width:202px;height:44px;" name ="login" onClick="facebook_access_token()"  id="login" /> </td>
		<td><div id="login_info"></div></td>
	</tr>
<!--	<tr>
		<td><input type="button" style="background-image:url(facebook-live.png);width:202px;height:44px;" name ="login" onClick="facebook_live_videos()"  id="login" /> </td>
		<td><div id="live_info"></div> </td>
	</tr>
	<tr>
		<td><input type="button" value="Delete Live Videos" name ="login" onClick="facebook_delete_live()"  id="login" /></td>
		<td><div id="del_info"></div></td>
	</tr>
	-->

	</table>
	
	<br/>
</body>
</html>